package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.StoreAccount;
import cn.stylefeng.guns.modular.basic.mapper.StoreAccountMapper;
import cn.stylefeng.guns.modular.basic.model.params.StoreAccountParam;
import cn.stylefeng.guns.modular.basic.model.result.StoreAccountResult;
import  cn.stylefeng.guns.modular.basic.service.StoreAccountService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 门店账户 服务实现类
 * </p>
 *
 * @author wp
 * @since 2025-08-27
 */
@Service
public class StoreAccountServiceImpl extends ServiceImpl<StoreAccountMapper, StoreAccount> implements StoreAccountService {

    @Override
    public void add(StoreAccountParam param){
        StoreAccount entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StoreAccountParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StoreAccountParam param){
        StoreAccount oldEntity = getOldEntity(param);
        StoreAccount newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StoreAccountResult findBySpec(StoreAccountParam param){
        return null;
    }

    @Override
    public List<StoreAccountResult> findListBySpec(StoreAccountParam param){
        return baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(StoreAccountParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public StoreAccountResult findListByStoreId(Integer storeId) {
        return baseMapper.findListByStoreId(storeId);
    }

    @Override
    public void updateByAccount(StoreAccountParam storeAccountParam) {
        this.baseMapper.updateByAccount(storeAccountParam);
    }

    @Override
    public StoreAccount getByAccount(Integer account, Integer storeId) {
        return this.baseMapper.getByAccount(account,storeId);
    }

    private Serializable getKey(StoreAccountParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StoreAccount getOldEntity(StoreAccountParam param) {
        return this.getById(getKey(param));
    }

    private StoreAccount getEntity(StoreAccountParam param) {
        StoreAccount entity = new StoreAccount();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
